package com.gsj.common.pojo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: clms
 * @description: 公共响应对象
 * @author: 郭书江
 * @create: 2021-07-16 19:33
 **/
@Data
@AllArgsConstructor
@ApiModel(value = "公共响应类")
public class R {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> result = new HashMap<String, Object>();

    //把构造方法私有
    private R() {}

    //成功静态方法
    public static R ok() {
        R r = new R();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS.getCode()); //把枚举类型理解为常量
        r.setMessage("成功");
        return r;
    }

    //失败静态方法
    public static R error() {
        R r = new R(); //单例模式 （1）构造方法 （2）声明一个全局静态对象 （3）创建一个静态方法
        r.setSuccess(false);
        r.setCode(ResultCode.ERROR.getCode());
        r.setMessage("失败");
        return r;
    }

    public R success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public R message(String message){
        this.setMessage(message);
        return this;
    }

    public R code(Integer code){
        this.setCode(code);
        return this;
    }

    public R put(String key, Object value){
        this.result.put(key, value);
        return this;
    }

    public R put(Map<String, Object> map){
        this.setResult(map);
        return this;
    }

}